package com.tianzeculture.stagebao.activity.artist.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.ArtistEditVideoActivity;
import com.tianzeculture.stagebao.activity.video.ModifyVideoActivity;
import com.tianzeculture.stagebao.bean.ArtistsBean;
import com.tianzeculture.stagebao.bean.ImageBean;
import com.tianzeculture.stagebao.bean.ListpBean;
import com.tianzeculture.stagebao.video.PlayerActivity;
import com.tianzeculture.stagebao.view.SqImageView;

import java.util.List;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/5/29 0029.
 */

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.RecyclerViewHolder> {
    Context context;
    List<ImageBean> artistsBeans;
    boolean isMine;

    public VideoAdapter(Context context, List<ImageBean> artistsBeans) {
        this.context = context;
        this.artistsBeans = artistsBeans;
    }

    public VideoAdapter(Context context, List<ImageBean> artistsBeans, boolean isMine) {
        this.context = context;
        this.artistsBeans = artistsBeans;
        this.isMine = isMine;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_artist_video, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return artistsBeans.size();
    }


    class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView toEditVideo;
        ImageView ivVideo;
        TextView tvTitle;
        TextView tvContent;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            toEditVideo = itemView.findViewById(R.id.tvToEditVideo);
            ivVideo = itemView.findViewById(R.id.ivVideo);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvContent = itemView.findViewById(R.id.tvContent);
        }

        public void setData(final int position) {
            if (isMine) toEditVideo.setVisibility(View.VISIBLE);
            else toEditVideo.setVisibility(View.INVISIBLE);
            tvTitle.setText(artistsBeans.get(position).getTitle());
            tvContent.setText(artistsBeans.get(position).getTitle1());
            Glide.with(context).load(PICTURE_URL + artistsBeans.get(position).getIndexPath()).error(R.mipmap.test).placeholder(R.mipmap.test).into(ivVideo);
            ivVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent_play = new Intent(context, PlayerActivity.class);
                    if (!TextUtils.isEmpty(artistsBeans.get(position).getPath())) {
                        intent_play.putExtra("path", artistsBeans.get(position));
                        intent_play.putExtra("isArtist",true);
                    }
                    context.startActivity(intent_play);
                }
            });
            toEditVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Intent intent = new Intent(context, ArtistEditVideoActivity.class);
                    Intent intent = new Intent(context, ModifyVideoActivity.class);
                    intent.putExtra("id", artistsBeans.get(position).getId());
                    context.startActivity(intent);
                }
            });
        }
    }

    public void setList(List<ImageBean> list) {
        artistsBeans = list;
        notifyDataSetChanged();
    }
}